<template>
	<el-container v-loading="loading">
		<el-aside width="200px" v-loading="showGroupLoading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="role_id"
							 :data="group" :props="groupsProps"
							 :current-node-key="''" :highlight-current="true"
							 :expand-on-click-node="false"
							 :filter-node-method="groupFilterNode"
							 @node-click="groupClick">
					</el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" v-auth="'admin:user:edit'" icon="el-icon-plus" @click="handleData('add')"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.username" placeholder="员工姓名" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="search.username = ''">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="编码" prop="user_id" width="100"></el-table-column>
					<el-table-column label="姓名" prop="username" width="150"></el-table-column>
					<el-table-column label="手机号" prop="mobile" width="150"></el-table-column>
					<el-table-column label="所属角色" prop="role_name" width="200"></el-table-column>
					<el-table-column label="状态" prop="status" width="80">
						<template #default="scope">
							<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :disabled="scope.row.allow_edit==0"
									   :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" prop="created_at" width="170"></el-table-column>
					<el-table-column label="创建人" prop="created_by" width="170"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
										   @click="handleData('show',scope.row, scope.$index)">查看
								</el-button>
								<el-button text type="primary" v-auth="'admin:user:edit'" :disabled="scope.row.allow_edit==0" size="small"
										   @click="handleData('edit',scope.row, scope.$index)">编辑
								</el-button>
								<el-button text type="primary" v-auth="'admin:user:edit'" :disabled="scope.row.allow_edit==0" size="small"
										   @click="deleteAdminUser('delete',scope.row, scope.$index)">删除
								</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save.vue'

	export default {
		name: 'user',
		components: {
			saveDialog
		},
		data() {
			return {
				dialog: {
					save: false
				},
				showGroupLoading: false,
				loading: false,
				groupFilterText: '',
				group: [],
				groupsProps: {
					value: "role_id",
					label: "name"
				},
				apiObj: this.$API['admin.user'].list,
				selection: [],
				search: {
					staff_name: null
				}
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			handleData(mode, row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open(mode).setData(row)
				})
			},
			//删除
			deleteAdminUser(mode, row) {
				console.log(mode, row);
				this.$confirm(`注：确认删除员工吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '确定',
					confirmButtonClass: 'el-button--danger'
				}).then(() => {
					this.$API['admin.user'].deleteAdminUser.post({user_id: row.user_id})
						.then((res) => {
							if (res.code == 0) {
								this.$refs.table.refresh()
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {

						});
				}).catch(() => {
				})

			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//表格内开关
			changeSwitch(val, row) {
				row.$switch_status = true;
				this.$API['admin.user'].changeStatus.post({status: val, user_id: row.user_id})
					.then((res) => {
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.$message.success("操作成功")
							row.status = val;
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {
						row.$switch_status = false;
					});
			},
			//加载树数据
			async getGroup() {
				this.showGrouploading = true;
				let res = await this.$API['admin.role'].list.get();
				this.showGrouploading = false;
				let allNode = {role_id: '', name: '所有'}
				res.data.list.unshift(allNode);
				this.group = res.data.list;
			},
			//树过滤
			groupFilterNode(value, data) {
				if (!value) return true;
				return data.role_name.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data) {
				let params = {
					role_id: data.role_id
				}
				this.$refs.table.reload(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess() {
				this.dialog.save = false
				this.$refs.table.reload()
			}
		}
	}
</script>

<style>
</style>
